<template>
  <div class='questions-container'>
    <el-card>
      <questions-pagetools>
        <template #left>
          <el-form size="small">
            <el-form-item label-width="80px" label="关键字">
                <el-input placeholder="根据编号搜索" v-model="keyword"></el-input>
            </el-form-item>
          </el-form>
        </template>
         <template #right>
          <el-row align="top">
            <el-button size="small" @click="keyword=''">清除</el-button>
            <el-button size="small" type="primary" @click="hSearch">搜索</el-button>
          </el-row>
        </template>
      </questions-pagetools>
      <el-alert
      :title="alertText"
      type="info"
      class="alert"
      :closable="false"
      show-icon
      ></el-alert>
       <el-table
      :header-cell-style="{background:'#fafafa'}"
      :data="radomList"
      style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="220">
        </el-table-column>
        <el-table-column
          label="题型"
          width="80">
          <template #default="{row}">
           {{formatQuestionType(row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column

          label="题目编号"
          width="220">
          <template #default="{row}">
            <div v-for="item in row.questionIDs" :key="item.number"><a href="#" style="color: rgb(0, 153, 255);" @click.prevent="hPreview(item.id)">{{item.number}}</a></div>
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          width="180"
          >
          <template #default="{row}">
           {{formatDate(row.addTime)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
          width="165">
        </el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
          width="160">
        </el-table-column>
        <el-table-column
          prop="userName"
          label="录入人"
          width="160">
        </el-table-column>

        <el-table-column
          label="操作"
          >
          <template #default="{row}">
            <el-button size="small" type="danger" icon="el-icon-delete" circle @click="hDel(row.id)" plain title="删除"></el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          background
          @current-change="handleCurrentChange"
          :current-page="q.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="q.pagesize"
          layout="prev,pager,next,sizes,jumper"
          :total="total">
        </el-pagination>
      </el-row>
    </el-card>
     <el-dialog title="题目预览" :visible.sync="showDialog" width="900px">
      <questions-preview :curId="curId" v-if="showDialog"></questions-preview>
      <span slot="footer" class="dialog-footer" >
        <div class="close">
          <el-button @click="showDialog = false"  type="primary">关闭</el-button>
        </div>
      </span>
   </el-dialog>
  </div>
</template>

<script>

import * as questions from '@/api/hmmm/questions.js'
import { questionType } from '@/api/hmmm/constants.js'

import moment from 'moment'
const questionTypeMnus = {}
questionType.forEach(item => {
  questionTypeMnus[item.value] = item.label
})
export default {

  data () {
    return {
      keyword: '',
      alertText: '共12条数据',
      showDialog: false,
      q: {
        page: 1,
        pagesiez: 10
      },
      total: 10,
      radomList: []
    }
  },
  created () {
    this.loadRadomList()
  },
  methods: {
    hSearch () {
      if (this.keyword) {
        this.q.keyword = this.keyword
      }
      this.loadRadomList()
      // this.keyword = ''
    },
    hDel (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const index = this.radomList.findIndex(item => item.id === id)
        await questions.removeRandoms({ id })
        this.radomList.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadRadomList()
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    hPreview (id) {
      console.log(id)
      this.curId = id
      this.showDialog = true
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.q.pagesize = val
      this.q.page = 1
      this.loadRadomList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.q.page = val
      this.loadRadomList()
    },
    async loadRadomList () {
      try {
        const res = await questions.randoms(this.q)
        console.log(res)
        if (res.status === 200) {
          this.total = res.data.counts
          this.alertText = `共${this.total}条数据`
          this.radomList = res.data.items
        }
      } catch (error) {
        console.log(error)
      }
    },
    formatQuestionType (id) {
      return questionTypeMnus[id] || ''
    },
    formatDate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'>
.questions-container{
    padding: 0 10px;
    margin: 10px 0;
    .alert {
      margin-bottom:15px ;
      }
     .pagination{
    margin: 20px 0;
  }
    }
</style>
